package com.rzgeo.rzmaplib;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.data.QueryParameters;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.rzgeo.rzmaplib.util.RmlUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Created by zhanghang on 2018/1/27.
 */

public abstract class RmlFeatureLayerQueryHelper {
    private int _count;
    private ArrayList<QueryResult>_results;
    public RmlFeatureLayerQueryHelper(Iterable<? extends Layer> layers, Point mp) {
        _results=new ArrayList<>();

        _count=0;
        for(Layer l:layers){
            if(l instanceof FeatureLayer){
                _count++;
            }
        }

        QueryParameters qp=new QueryParameters();
        qp.setGeometry(mp);
        for(final Layer l:layers){
            if(l instanceof FeatureLayer){
                final FeatureLayer fl= (FeatureLayer) l;
                final ListenableFuture<FeatureQueryResult> lf = fl.getFeatureTable().queryFeaturesAsync(qp);
                lf.addDoneListener(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Iterator<Feature> iterator = lf.get().iterator();
                            ArrayList<Feature>features=new ArrayList<>();
                            while (iterator.hasNext()) {
                                Feature feature = iterator.next();
                                features.add(feature);
                            }
                            _results.add(new QueryResult(features.toArray(new Feature[0]),fl,true,null));
                            checkResult();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            _results.add(new QueryResult(null,fl,false,e.getLocalizedMessage()));
                            checkResult();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                            _results.add(new QueryResult(null,fl,false,e.getLocalizedMessage()));
                            checkResult();
                        }finally {

                        }
                    }
                });
            }
        }
    }

    protected void checkResult(){
        if(_results.size()<_count)
            return;

        onQueryResult(_results.toArray(new QueryResult[0]));
    }

    public static class QueryResult{
        public final Feature[]features;
        public final FeatureLayer layer;
        public final boolean success;
        public final String errMsg;

        public QueryResult(Feature[] features, FeatureLayer layer,boolean success,String errMsg) {
            this.features = features;
            this.layer = layer;
            this.success = success;
            this.errMsg = errMsg;
        }
    }

    public abstract void onQueryResult(QueryResult[]queryResult);
}
